Week 10 of 16

Watch: Exceptions and Logging

Real tools don't crash with tracebacks — they catch errors, log what happened, and keep running

Day 46 75 minutes Watch

Day 46 of 80

The Point of This Week

You've built a real app. It has classes, a Flask web interface, and talks to the Claude API. But right now, if something goes wrong — a file is missing, the user enters bad data, the API is down — your app crashes with a Python traceback. That's fine for learning. It's not fine for software anyone else will use.

This week is about making your Prompt Vault bulletproof. Two tools make that possible:

print() vs logging

print() is for users — it shows output in the terminal right now. logging is for you the developer — it writes a timestamped record of everything that happened. When production software breaks at 2am, you can't be there watching terminal output. You open the log file and read the history of exactly what the app did, in order, with timestamps. That's what logging gives you.

Rule of thumb: if you're adding output to understand what your code is doing, use logging.debug() or logging.info(), not print().

Today's Videos

Three videos from Corey Schafer — consistently the clearest Python instructor on YouTube. Watch them in order. The try/except video (~18 min) establishes the foundation; the two logging videos build on each other.

# Video Length What You'll Learn
1 Corey Schafer — Python Tutorial: Using Try/Except Blocks ~18 min The full try/except/else/finally system with real examples
2 Corey Schafer — Logging Basics ~20 min basicConfig, log levels, formatting messages
3 Corey Schafer — Logging Advanced ~15 min File handlers, console handlers, logging across multiple modules
How to Watch

Watch at 1.25x speed if Corey's pace feels slow. Keep a text file open and jot down any questions that come up — Day 47 will answer them hands-on in Jupyter. Don't try to code along with the videos today; focus on understanding the concepts first.

The try/except/else/finally Pattern

After watching the first video, this pattern should make sense. Here's a reference to come back to:

Four Parts, Four Purposes
pattern.py Python
try:
    result = risky_operation()   # might raise an exception
except ValueError as e:
    print(f"Bad value: {e}")       # handle specific error type
except (FileNotFoundError, PermissionError) as e:
    print(f"File problem: {e}")    # catch multiple types at once
else:
    print("Success!")              # only runs if try succeeded
    save(result)
finally:
    cleanup()                      # always runs — open files, DB connections
The else block is optional but valuable — it keeps your success-path code separate from your error-handling code. The finally block is also optional, but important whenever you're working with resources that need to be released.

Log Levels

Python's logging module has five levels, in order of severity. You set a minimum level — messages below that threshold are silently ignored.

Level When to Use It Example
DEBUG Nitty-gritty details for diagnosing problems. Very verbose. Usually disabled in production. "Loading file: prompts.json"
INFO Normal operations — things that are supposed to happen. "Loaded 12 prompts successfully"
WARNING Something unexpected happened but the app is still working. "Platform 'Pika' not recognized, using default"
ERROR Something broke. The operation failed, but the app may continue. "Failed to save prompts.json: permission denied"
CRITICAL The app can't continue running. Very serious. "Database connection lost, shutting down"
A Common Pattern

During development: set the minimum to DEBUG so you see everything. In production: set it to WARNING or INFO to keep log files from growing too large. The key is you can change the verbosity by changing one number — no code changes needed.

Why This Matters for the Prompt Vault

Right now, if someone tries to add a prompt with a bad platform name, your app raises a ValueError and crashes. If the JSON file gets corrupted, the whole app stops. If you're running the Flask server and something goes wrong, you see nothing — the request just fails silently.

By the end of this week, you'll have:

The Professional Mindset

Production software doesn't just work when everything goes right. It fails gracefully when things go wrong, logs what happened, and keeps running. After this week, your Prompt Vault will behave like professional software.

End of Day Checklist

Tomorrow

Day 47 is Read + Jupyter. You'll read the Real Python logging guide and then work through five Jupyter cells covering exception types, custom exceptions, and setting up dual-handler logging. Hands-on practice with everything you watched today.